package com.thiny.android.app.tsms.ui;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.thiny.android.app.tsms.R;
import com.thiny.android.app.tsms.adapter.MessageAdapter;
import com.thiny.android.app.tsms.logic.ShareLogic;
import com.thiny.android.app.tsms.model.Message;
import com.thiny.android.app.tsms.util.AccessTokenKeeper;
import com.thiny.android.app.tsms.util.MessageHelper;
import com.thiny.android.app.tsms.util.Utils;
import com.weibo.sdk.android.Oauth2AccessToken;
import com.weibo.sdk.android.WeiboAuthListener;
import com.weibo.sdk.android.WeiboDialogError;
import com.weibo.sdk.android.WeiboException;
import com.weibo.sdk.android.net.RequestListener;

public class MainActivity extends Activity implements OnItemClickListener,
		OnItemLongClickListener {

	private ShareLogic mShareLogic;
	private Handler mHandler;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		// List<String> contens = Arrays.asList(getResources().getStringArray(
		// R.array.spring_festival));
		String[] contents = getResources().getStringArray(
				R.array.spring_festival);
		List<Message> msgs = new ArrayList<Message>();
		String title = getString(R.string.spring_festival);
		for (String content : contents) {
			Message msg = new Message(MessageHelper.TYPE_SPRING_FESTIVAL,
					title, content);
			msgs.add(msg);
		}
		ListView listView = getListView();
		listView.setAdapter(new MessageAdapter(this, msgs));
		listView.setOnItemClickListener(this);
		listView.setOnItemLongClickListener(this);
	}

	private ListView getListView() {
		return (ListView) findViewById(R.id.list);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (mShareLogic != null) {
			mShareLogic.weiboAuthorizeCallBack(requestCode, resultCode, data);
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		MessageAdapter adapter = (MessageAdapter) parent.getAdapter();
		Message msg = adapter.getItem(position);
		Utils.startSmsActivity(this, msg.getContent(), "10086");
	}

	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view,
			int position, long id) {
		if (mShareLogic == null) {
			mShareLogic = ShareLogic.getInstance();
		}
		initHandler();
		MessageAdapter adapter = (MessageAdapter) parent.getAdapter();
		final Message msg = adapter.getItem(position);

		Oauth2AccessToken accessToken = AccessTokenKeeper.readAccessToken(this);
		if (accessToken.isSessionValid()) {
			showShareDialog(msg.getContent());
		} else {
			mShareLogic.makeSureAccessTokenUseful(this,
					new WeiboAuthListener() {

						@Override
						public void onWeiboException(WeiboException arg0) {
						}

						@Override
						public void onError(WeiboDialogError arg0) {
						}

						@Override
						public void onComplete(Bundle arg0) {
							showShareDialog(msg.getContent());
						}

						@Override
						public void onCancel() {
						}
					});
		}
		return true;
	}

	private void showShareDialog(final String content) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("微博分享").setMessage(content);
		builder.setPositiveButton("分享", new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				RequestListener listener = new RequestListener() {

					@Override
					public void onIOException(IOException arg0) {
						log("分享异常~");
						android.os.Message msg = android.os.Message.obtain();
						msg.what = 0;
						mHandler.sendMessage(msg);

					}

					@Override
					public void onError(WeiboException arg0) {
						android.os.Message msg = android.os.Message.obtain();
						msg.what = 1;
						mHandler.sendMessage(msg);
						log("分享错误~" + arg0.getStatusCode());
						if (arg0.getStatusCode() == 403) {
							AccessTokenKeeper.clear(MainActivity.this);
						}

					}

					@Override
					public void onComplete(String arg0) {
						android.os.Message msg = android.os.Message.obtain();
						msg.what = 2;
						mHandler.sendMessage(msg);

					}
				};
				mShareLogic.shareToWeibo(MainActivity.this, content, listener);
			}
		});
		builder.create().show();
	}

	private void initHandler() {
		if (mHandler == null) {
			mHandler = new Handler() {

				@Override
				public void handleMessage(android.os.Message msg) {
					switch (msg.what) {
					case 0:
						showToast("分享异常~");
						break;
					case 1:
						showToast("分享错误~");
						break;
					case 2:
						log("分享成功~");
						showToast("分享成功~");
						break;

					}

				}

			};
		}
	}

	private void showToast(String text) {
		Toast.makeText(this, text, Toast.LENGTH_LONG).show();
	}

	private void log(String text) {
		Log.e("", text);
	}
}
